package org.tp8.sampleapp.view;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.primefaces.model.chart.PieChartModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.tp8.sampleapp.exc.SampleAppException;
import org.tp8.sampleapp.persistence.repository.CourseRepository;
import org.tp8.sampleapp.persistence.repository.StudentRepository;
import org.tp8.sampleapp.persistence.repository.TeacherRepository;

/**
 * The Class ChartBean.
 */
@ManagedBean(name = "chartBean")
@RequestScoped
public class ChartBean extends AbstractBean {

	@Autowired
	private CourseRepository courseRepository;

	@Autowired
	private StudentRepository studentRepository;

	@Autowired
	private TeacherRepository teacherRepository;

	/**
	 * Gets the pie model.
	 * 
	 * @return the pie model
	 */
	public PieChartModel getPieModel() {
		Long courses;
		try {
			courses = getCourseFacade().count();
		} catch (SampleAppException e) {
			courses = 0L;
		}

		Long students;
		try {
			students = getStudentFacade().count();
		} catch (SampleAppException e) {
			students = 0L;
		}

		Long teachers;
		try {
			teachers = getTeacherFacade().count();
		} catch (SampleAppException e) {
			teachers = 0L;
		}

		PieChartModel pieModel = new PieChartModel();
		pieModel.set(getMessage("courses"), courses);
		pieModel.set(getMessage("students"), students);
		pieModel.set(getMessage("teachers"), teachers);

		return pieModel;
	}

}